package com.shijunhao.graduation.controller.system;

import com.shijunhao.graduation.annotations.OperationLog;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.dto.ModifyPassDto;
import com.shijunhao.graduation.domain.dto.UserRegistry;
import com.shijunhao.graduation.domain.entity.OrgUser;
import com.shijunhao.graduation.domain.vo.CurrentUserVo;
import com.shijunhao.graduation.properties.OperationTarget;
import com.shijunhao.graduation.properties.OperationType;
import com.shijunhao.graduation.service.business.IBusinessUserInfoService;
import com.shijunhao.graduation.service.system.IOrgUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: shijunhao
 * @date: 2022/3/3 12:10
 * @description:
 */
@RestController
@Api(tags = {"权限验证"})
public class AuthController {

    @Resource
    private IBusinessUserInfoService businessUserInfoService;
    @Resource
    private IOrgUserService orgUserService;

    @PostMapping({"/login"})
    @ApiOperation(
            value = "登录",
            consumes = "application/x-www-form-urlencoded"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
            name = "username",
            value = "用户名",
            dataTypeClass = String.class,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "password",
            value = "密码",
            dataTypeClass = String.class,
            paramType = "query"
    )})
    public ResultCode<String> login() {
        return ResultCode.success("登录成功！");
    }

    @GetMapping({"/logout"})
    @ApiOperation("登出")
    @ApiImplicitParam()
    public ResultCode<Void> logout() {
        return ResultCode.success("退出成功！");
    }


    @PostMapping({"/admin/registry"})
    @ApiOperation(value = "管理员注册")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResultCode<Long> adminRegistry(@RequestBody OrgUser user){
        return ResultCode.success("注册成功",orgUserService.registry(user));
    }

    @PostMapping({"/registry"})
    @ApiOperation(value = "用户注册")
    public ResultCode<Long> userRegistry(@RequestBody UserRegistry user){
        OrgUser orgUser = new OrgUser();
        BeanUtils.copyProperties(user,orgUser);
        orgUser.setRole(1);
        orgUser.setEnable(true);
        return ResultCode.success("注册成功",orgUserService.registry(orgUser));
    }

    @GetMapping({"/currentUser"})
    @ApiOperation(value = "当前用户信息")
    public ResultCode<CurrentUserVo> currentUser(){
        return ResultCode.success(businessUserInfoService.getCurrentOrgUser());
    }

    @PostMapping({"/modifyPass"})
    @ApiOperation(value = "修改密码")
    @OperationLog(target = OperationTarget.USER,type = OperationType.UPDATE,description = "用户修改密码")
    public ResultCode<Boolean> registry(@RequestBody ModifyPassDto dto){
        OrgUser currentUser = businessUserInfoService.getCurrentUser();
        return ResultCode.success("修改成功",orgUserService.updatePass(currentUser.getId(),dto));
    }
}
